import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
    {
        path: "/",
        // 因为包含了 children 属性，所以需要在 component 组件内加一个 <router-view />
        component: () => import("../views/Index.vue"),
        children: [
            {
                path: "/bwl/list",
                component: () => import("../views/BwlList.vue"),
            },
            {
                path: "/bwl/add",
                component: () => import("../views/BwlAdd.vue"),
            },
            {
                path: "/mine",
                component: () => import("../views/Mine.vue"),
            },
        ],
    },
    {
        path: "/bwl/:id",
        component: () => import("../views/BwlDetail.vue"),
    },
    {
        path: "/login",
        component: () => import("../views/Login.vue"),
    },
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === "/login" || window.sessionStorage.getItem("token")) {
        next();
    } else {
        next("/login");
    }
});

export default router;
